package com.kkb.service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.TeamMapper;
import com.kkb.pojo.Team;
import com.kkb.pojo.TeamExample;
import com.kkb.vo.QueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TeamService {

    @Resource
     private TeamMapper teamMapper;
    /**
     * @param pageNum 当前页
     * @param pageSize 当前页的数量
     * @param vo 封装多条件查询的对象
     * @return 返回分页对象
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public PageInfo<Team> queryByPage(Integer pageNum, Integer pageSize, QueryVo vo){
        //多条件查询
        TeamExample teamExample = new TeamExample();
        //创建条件容器
        TeamExample.Criteria criteria = teamExample.createCriteria();
        if (vo!=null){
            if (vo.getTeamName()!=null && !"".equals(vo.getTeamName().trim())){
                criteria.andTeamNameLike("%"+vo.getTeamName()+"%");
            }
            if (vo.getChineseName()!=null &&  !"".equals(vo.getChineseName().trim())){
                criteria.andChineseNameLike("%"+vo.getChineseName()+"%");
            }
            if (vo.getCoach()!=null &&  ! "".equals(vo.getCoach().trim())){
                criteria.andCoachLike("%"+vo.getCoach()+"%");
            }
            //起始时间
            if (vo.getBeginDate()!=null){
                criteria.andCreateTimeGreaterThanOrEqualTo(vo.getBeginDate());
            }
            //结束时间
            if (vo.getEndDate()!=null){
                criteria.andCreateTimeLessThanOrEqualTo(vo.getEndDate());
            }
            if (vo.getArea()!=null && vo.getArea()!=-1){
                criteria.andAreaEqualTo(vo.getArea());
            }
        }
        //分页
        PageHelper.startPage(pageNum,pageSize);
        List<Team> teams = teamMapper.selectByExample(teamExample);
        return new PageInfo<>(teams); //将查询到的数据封装在 PageInfo中
    }

    /**
     * 事务的添加
     * @param team
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int addTeam(Team team){
        return teamMapper.insertSelective(team);
    }

    /**
     * @param teamId
     * @return 返回查询到的team
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public Team queryById(int teamId){
        return teamMapper.selectByPrimaryKey(teamId);
    }

    /**
     * 根据主键去更新
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int updateTeam(Team team){
        return teamMapper.updateByPrimaryKeySelective(team);
    }

    /**
     * 根据teamId,进行逻辑删除
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int deleteTeam(int teamId){
        Team team = teamMapper.selectByPrimaryKey(teamId);
        team.setIsDel(1);
        return teamMapper.updateByPrimaryKeySelective(team);
    }

}
